// ---------------------------------------------------------------------
//
// -- 游戏Util
// -- author £
//
// ---------------------------------------------------------------------

const MathUtil = require('MathUtil');

const GRID_H_W = 36.5,
    GRID_H_H = 41.5,
    COLOR_LIST = [
        '#ff89b5',
        '#ffdc89',
        '#71e096',
        '#f5a26f',
        '#cff381',
        '#898cff',
    ],
    MAX_COLOR_COUNT = 6;

let _dVec = [],
    _laserData,
    _bombData;

let HexagonUtil = {
    BASE_SCORE: 40,
    ERASE_SCORE: 20,
    startup: function () {
        _dVec[0] = {x:GRID_H_W * 2, y: 0};
        _dVec[1] = {x:GRID_H_W, y: -GRID_H_H * 1.5};
        _dVec[2] = {x:-GRID_H_W, y: -GRID_H_H * 1.5};
        _dVec[3] = {x:-GRID_H_W * 2, y: 0};
        _dVec[4] = {x:-GRID_H_W, y: GRID_H_H * 1.5};
        _dVec[5] = {x:GRID_H_W, y: GRID_H_H * 1.5};
        _laserData = [
            [
                {start: 0, end: 4}, {start: 5, end: 10}, {start: 11, end: 17},
                {start: 18, end: 25}, {start: 26, end: 34}, {start: 35, end: 42},
                {start: 43, end: 49}, {start: 50, end: 55}, {start: 56, end: 60}
            ],
            [
                {start: 4, end: 34}, {start: 3, end: 42}, {start: 2, end: 49},
                {start: 1, end: 55}, {start: 0, end: 60}, {start: 5, end: 59},
                {start: 11, end: 58}, {start: 18, end: 57}, {start: 26, end: 56}
            ],
            [
                {start: 0, end: 26}, {start: 1, end: 35}, {start: 2, end: 43},
                {start: 3, end: 50}, {start: 4, end: 56}, {start: 10, end: 57},
                {start: 17, end: 58}, {start: 25, end: 59}, {start: 34, end: 60}
            ],
        ];
        _bombData = [
            [4, 4], [4, 5], [5, 5], 
            [4, 3], [4], [5], [5, 0],
            [3, 3], [3], [0], [0, 0],
            [3, 2], [2], [1], [0, 1],
            [2, 2], [1, 2], [1, 1]
        ];
    },

    getBombPath: function () {
        return _bombData;
    },

    createLasers: function () {
        let d = require('MathUtil').getIntRandom(0, 2),
            index0 = require('MathUtil').getIntRandom(0, 7),
            result = [];
            
        for (let i = 0; i < 2; ++i) {
            result[i] = _laserData[d][index0 + i];
            result[i].d = d;
        }

        return result;
    },

    getLocation: function (d) {
        return _dVec[d];
    },

    createItem: function (fixStep) {
        let r = Math.random(), step = 0;

        // if (r <= 0.03) {
        //     step = 1;
        // } else if (r > 0.03 && r <= 0.15) {
        //     step = 2;
        // } else if (r > 0.15 && r <= 0.5) {
        //     step = 3;
        // } else {
        //     step = 4;
        // }

        if (r <= 0.03) {
            step = 1;
        } else if (r > 0.03 && r <= 0.2) {
            step = 2;
        } else if (r > 0.2 && r <= 0.55) {
            step = 3;
        } else {
            step = 4;
        }

        if (fixStep !== null && fixStep !== undefined && fixStep > 0)
            step = fixStep;

        let nodeLocation = [{x: 0, y: 0}],
            nodePath = [[]], tmpPath,
            i, len = step - 1, j, k, len2,
            startL, startIndex, curDs, curD,
            tmpArr;

        for (i = 0; i < len; ++i) {
            tmpPath = nodeLocation.concat();

            while (tmpPath.length) {
                startIndex = tmpPath.length - 1;

                while (startIndex) {
                    if (Math.random() >= 0.5)
                        break;
                    
                    --startIndex;
                }

                // startIndex = MathUtil.getIntRandom(0, tmpPath.length - 1);
                startL = tmpPath[startIndex];
                curDs = [];  

                for (j = 0; j < 6; ++j) {
                    curD = this.getLocation(j);
                    curDs[j] = {
                        d: j,
                        x: startL.x + curD.x,
                        y: startL.y + curD.y,
                    }
                }

                // console.log('nodeLocation:' + JSON.stringify(nodeLocation));
                // console.log('before:' + JSON.stringify(curDs));
                for (j = 0; j < curDs.length; ++j) {
                    len2 = nodeLocation.length;

                    for (k = 0; k < len2; ++k) {
                        if (this.equalLocation(nodeLocation[k], curDs[j])) {
                            curDs.splice(j--, 1);
                            break;
                        }
                    }
                }

                if (!curDs.length) {
                    tmpPath.splice(startIndex, 1);
                    continue;
                }

                curD = curDs[MathUtil.getIntRandom(0, curDs.length - 1)];

                // if (step === 4) {
                //     console.log('i:', i);
                //     console.log('startIndex:', startIndex);
                //     console.log('curDs:', curDs);
                //     console.log('curD:', curD);
                // }

                nodeLocation[nodeLocation.length] = {
                    x: curD.x,
                    y: curD.y
                };
                tmpArr = nodePath[nodePath.length] = nodePath[startIndex].concat();
                tmpArr[tmpArr.length] = curD.d;
                break;
            }
        }

        let minX, maxX, minY, maxY, 
            offx, offy;

        len = nodeLocation.length;
        minX = maxX = nodeLocation[0].x;
        minY = maxY = nodeLocation[0].y;

        for (i = 1; i < len; ++i) {
            startL = nodeLocation[i];

            if (startL.x > maxX) maxX = startL.x;
            if (startL.y > maxY) maxY = startL.y;
            if (startL.x < minX) minX = startL.x;
            if (startL.y < minY) minY = startL.y;
        }

        offx = -(maxX - minX) * 0.5 - minX;
        offy = -(maxY - minY) * 0.5 - minY;

        for (i = 0; i < len; ++i) {
            nodeLocation[i].x += offx;
            nodeLocation[i].y += offy;
        }
        
        let result = {
            nodeLocation: nodeLocation,
            nodePath: nodePath,
            colorIndex: MathUtil.getIntRandom(0, MAX_COLOR_COUNT - 1)
        };

        // console.log(result);
        return result;
    },

    equalLocation: function (l1, l2) {
        return (l1.x === l2.x && l1.y === l2.y);
        // return (l1.x === l2.x && l1.y === l1.y);
    },

    getEraseScore: function (erases) {
        let deltaScore = 0, 
            i, len = erases.length, j, len2,
            count = 0;

        for (i = 0; i < len; ++i) {
            len2 = erases[i].length;
            for (j = 0; j < len2; ++j) {
                count++;
            }
        }

        deltaScore = Math.floor(Math.pow(len, 0.625) * 
            (this.BASE_SCORE + (count * this.ERASE_SCORE)));
        return deltaScore;
    },
};

module.exports = HexagonUtil;

HexagonUtil.startup();